'use strict';

const conn = require('../../libs/Conn');
const _ = require('underscore');

module.exports = async function (params) {
    let certificateObj = {};
    let sql = `SELECT engineering_id,file_no, DATE_FORMAT(submission_date,'%Y-%m-%d') AS submission_date,DATE_FORMAT(release_date,'%Y-%m-%d') AS release_date,status FROM quality_file WHERE engineering_id = ?;SELECT engineering_id,certificate_no,user_id,DATE_FORMAT(receipt_date,'%Y-%m-%d') AS receipt_date,certificate_type,unit_type,status FROM certificate_file WHERE engineering_id = ?`;
    let rec = await conn(sql,[params.engineering_id,params.engineering_id]);
    certificateObj.quality = {};
    certificateObj.quality = rec[0][0];
    let certificateList = rec[1];
     certificateList = _.groupBy(certificateList,'certificate_type');
    certificateObj.certificate={};
       for(let type in certificateList){
         let certificateTypeList = certificateList[type];
        if(type === '1' ){
            certificateObj.certificate.authoriz = certificateTypeList
        }else{
            certificateObj.certificate.commitment = certificateTypeList
        }
     }
    return {success: true, data: certificateObj};

};